package com.start.workflow.core.mappers;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.workflow.act.StartActMyHandlerAuthTask;
import com.start.workflow.act.StartActWaitAuthTask;
import com.start.workflow.entity.StartActTaskEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface StartActTaskMapper {

    /**
     * 我的待办查询
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT  " +
            "  a.*   " +
            "FROM  " +
            "  (  " +
            "  SELECT  " +
            "    sab.*,  " +
            "    art.ID_ task_id,  " +
            "    art.NAME_ task_name,  " +
            "    art.TASK_DEF_KEY_ task_def_key,  " +
            "    art.CREATE_TIME_ task_start_time,  " +
            "    art.PRIORITY_ task_priority ,  " +
            "    aril.TYPE_ task_identity_link_type,  " +
            "    aril.USER_ID_ task_candidate_user_key  " +
            "  FROM  " +
            "    start_act_business sab  " +
            "  INNER JOIN act_ru_task art ON art.PROC_INST_ID_ = sab.proc_inst_id   " +
            "  INNER JOIN act_ru_identitylink aril on aril.TASK_ID_=art.ID_  " +
            "  ) a ${ew.customSqlSegment}")
    Page<StartActWaitAuthTask> pageMyWaitAuthTask(@Param("page") IPage<StartActWaitAuthTask> page, @Param("ew") Wrapper<StartActWaitAuthTask> wrapper);

    /**
     * 我的已办
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    @Select("SELECT  " +
            "  a.*   " +
            "FROM  " +
            "  (  " +
            "  SELECT  " +
            "    sab.*,  " +
            "    aha.TASK_ID_ task_id,  " +
            "    aha.ACT_NAME_ task_name,  " +
            "    aha.ACT_ID_ task_def_key,  " +
            "    aha.START_TIME_ task_start_time,  " +
            "    aha.END_TIME_ task_end_time,  " +
            "    aha.ACT_TYPE_ ACT_TYPE_,  " +
            "    aha.ASSIGNEE_ task_auth_user_key  " +
            "  FROM  " +
            "    start_act_business sab  " +
            "    INNER JOIN act_hi_actinst aha ON aha.PROC_INST_ID_ = sab.proc_inst_id   " +
            "  ) a ${ew.customSqlSegment}")
    Page<StartActMyHandlerAuthTask> pageMyDoneTask(@Param("page") IPage<StartActMyHandlerAuthTask> page, @Param("ew") Wrapper<StartActMyHandlerAuthTask> queryWrapper);


    /**
     * 分页查询
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT  " +
            "  a.*   " +
            "FROM  " +
            "  (  " +
            "  SELECT  " +
            "    sab.*,  " +
            "    art.ID_ task_inst_id,  " +
            "    art.TASK_DEF_KEY_ task_def_key,  " +
            "    art.NAME_ task_name,  " +
            "    art.PRIORITY_ task_priority,  " +
            "    art.CREATE_TIME_ task_create_time   " +
            "  FROM  " +
            "    start_act_business sab  " +
            "  INNER JOIN act_ru_task art ON art.PROC_INST_ID_ = sab.proc_inst_id   " +
            "  ) a ${ew.customSqlSegment} ")
    Page<StartActTaskEntity> pageActTask(@Param("page") Page<StartActTaskEntity> page, @Param("ew") Wrapper<StartActTaskEntity> wrapper);

    @Select("select COUNT(ID_) " +
            "from act_ru_identitylink " +
            "WHERE TASK_ID_=#{taskId} " +
            "and USER_ID_=#{startUserKey} " +
            "and TYPE_=#{taskType}")
    long countTaskCandidateUser(@Param("taskId") String taskId, @Param("startUserKey") String startUserKey, @Param("taskType") String taskType);


}
